package com.example.productmanagerspringboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.productmanagerspringboot.data.entity.Demand;
import com.example.productmanagerspringboot.data.vo.DemandsVO;
import com.example.productmanagerspringboot.data.vo.Response;
import com.example.productmanagerspringboot.data.vo.ResponseWithData;
import com.example.productmanagerspringboot.mapper.DemandMapper;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/demand")
@Slf4j
@Api(tags = "需求")
public class DemandController {
    private final DemandMapper mapper;

    public DemandController(DemandMapper mapper) {
        this.mapper = mapper;
    }


    @GetMapping("/list")
    @Operation(summary = "展示需求")
    //http://localhost:8080/api/Demands/list?pageNumber=1&pageSize=10
//        @ApiImplicitParams(@ApiImplicitParam(paramType = "body", name = "photo", value = "图片文件", dataTypeClass = MultipartFile.class))
    public ResponseEntity<ResponseWithData<DemandsVO>> getDemands(@RequestParam(value = "pageNumber", defaultValue = "1") int pageNumber, @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Page<Demand> page = new Page<>(pageNumber, pageSize);
        Page<Demand> DemandPage = mapper.selectPage(page, null);
        List<Demand> records = DemandPage.getRecords();
        return ResponseEntity.ok(new ResponseWithData<>("success", 200, new DemandsVO(DemandPage.getTotal(), pageNumber, records)));
    }

    @GetMapping("/id/{id}")
    @Operation(summary = "获取指定ID的需求")
    //http://localhost:8080/api/Demand/0
    public ResponseEntity<ResponseWithData<Demand>> getDemandById(@PathVariable("id") int id) {
        Demand Demand = mapper.selectById(id);
        return ResponseEntity.ok(new ResponseWithData<>("success", 200, Demand));
    }

    @PostMapping("/add")
    @Operation(summary = "添加需求")
    public ResponseEntity<Response> addDemand(@Validated @RequestBody Demand Demand) {
        Demand.setOwnerId(1);
        log.info("addDemand:{}", Demand);
        int insert = mapper.insert(Demand);
        Response response = new Response();
        if (insert == 0) {
            response.setResultCode(500);
            response.setMessage("新增失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        } else {
            response.setResultCode(200);
            response.setMessage("新增成功");
            return ResponseEntity.ok().body(response);
        }
    }

    @PutMapping("/add")
    @Operation(summary = "修改需求")
    public ResponseEntity<Response> editDemand(@Validated @RequestBody Demand Demand) {
        log.info("editDemand:{}", Demand);
        int update = mapper.updateById(Demand);
        Response response = new Response();
        if (update == 0) {
            response.setResultCode(500);
            response.setMessage("修改失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        } else {
            response.setResultCode(200);
            response.setMessage("修改成功");
            return ResponseEntity.ok().body(response);
        }
    }

    @DeleteMapping("/delete")
    @Operation(summary = "修改需求")
    public ResponseEntity<Response> deleteDemand(@Validated @RequestBody Demand Demand) {
        log.info("deleteDemand:{}", Demand);
        int update = mapper.deleteById(Demand);
        Response response = new Response();
        if (update == 0) {
            response.setResultCode(500);
            response.setMessage("删除失败");
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        } else {
            response.setResultCode(200);
            response.setMessage("删除成功");
            return ResponseEntity.ok().body(response);
        }
    }
}
